﻿namespace AddressBook.Business.Contact
{
	#region "Using Statements"
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;

	using Csla;
	using Csla.Data;
	#endregion

	public class FamilyList : ReadOnlyListBase<FamilyList, FamilyInfo>
	{
		#region  "Factory Methods"

    public static FamilyList EmptyList()
    {
      return new FamilyList();
    }

    public static FamilyList GetFamilyList()
    {
      return DataPortal.Fetch<FamilyList>();
    }

		private FamilyList()
    { /* require use of factory methods */ }

    #endregion

		#region  "Data Access"

		private void DataPortal_Fetch()
		{
			RaiseListChangedEvents = false;

			using (var ctx = ContextManager<AddressBook.Business.Model.AddressBookDataContext>.GetManager(AddressBook.Business.Model.Database.AddressBook))
			{
				List<FamilyInfo> data = (from f in ctx.DataContext.Families
																	orderby f.Name
																	select new FamilyInfo(
																		f.Id,
																		f.Name
																		)).ToList<FamilyInfo>();

				this.IsReadOnly = false;

				this.AddRange(data);

				this.IsReadOnly = true;
			}

			RaiseListChangedEvents = true;
		}

		#endregion
	}
}